package com.medic.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.medic.entity.group.pain.SikerInfo;
import com.medic.service.interf.InitServiceInterface;
import com.medic.util.CacheUtil;
import com.medic.util.HttpUtil;
import com.medic.util.JsonUtil;
import com.medic.util.PropertiesUtil;
import com.medic.util.StringUtil;

@Service("initService")
public class InitServiceImpl implements InitializingBean, InitServiceInterface {

	@Resource(name = "medicService")
	private MedicServiceImpl medicService;

	@Resource(name = "sikerinfoService")
	private SikerinfoServiceImpl sikerinfoService;

	@Resource(name = "taskService")
	private TaskServiceImpl taskService;

	private final String tokenRes = "access_token";
	private final String ticketRes = "ticket";

	@Override
	public void afterPropertiesSet() throws Exception {
		// 初始化时候就装载资源文件、减少io操作
		CacheUtil.setConfig(PropertiesUtil.loadFile(StringUtil.RESOURCE));

		// 加载topic_pagesize
		CacheUtil.setTopic_pagesize(Integer.parseInt(CacheUtil.getConfig().getProperty("topic_pagesize").trim()));
		// 加载role_pagesize
		CacheUtil.setRole_pagesize(Integer.parseInt(CacheUtil.getConfig().getProperty("role_pagesize").trim()));
		// 加载reply_pagesize
		CacheUtil.setReply_pagesize(Integer.parseInt(CacheUtil.getConfig().getProperty("reply_pagesize").trim()));
		// 加载task_pagesize
		CacheUtil.setTask_pagesize(
				Integer.parseInt(CacheUtil.getConfig().getProperty(StringUtil.TASK_PAGE_SIZE).trim()));

		// 缓存用药提醒信息
		CacheUtil.setMedics(medicService.getAll());

		// 缓存入组信息
		cacheSikerinfo();
	}

	@Scheduled(fixedRate = (long) ((2 * 60 * 1000 * 60) - (10 * 1000)))
	@Override
	public void cacheWxData() throws Exception {
		String url = CacheUtil.getConfig().getProperty(StringUtil.TOKEN_URL);
		url = url + "grant_type=client_credential&appid=" + CacheUtil.getConfig().getProperty(StringUtil.APP_ID)
				+ "&secret=" + CacheUtil.getConfig().getProperty(StringUtil.APP_SECRET);
		String resJson = HttpUtil.getReq(url);
		String value = JsonUtil.getValue(resJson, tokenRes);
		if (value != null && !value.equals("")) {
			CacheUtil.setAccessToken(value);
			// System.out.println(value);
		}
		url = CacheUtil.getConfig().getProperty(StringUtil.TICKET_URL);
		url = url + "access_token=" + CacheUtil.getAccessToken() + "&type=jsapi";
		resJson = HttpUtil.getReq(url);
		value = JsonUtil.getValue(resJson, ticketRes);
		if (value != null && !value.equals("")) {
			CacheUtil.setJsapiTicket(value);
			// System.out.println(value);
		}
	}

	@Override
	public void cacheSikerinfo() throws Exception {
		List<SikerInfo> infos = sikerinfoService.getAll();
		HashMap<String, SikerInfo> map = new HashMap<String,SikerInfo>();
		for (SikerInfo info : infos) {
			if (info.getIsTitr() > 1) {
				long tsId = taskService.getTitrTask(info.getSikerId());
				info.setTrTsId(tsId);
			} 
			map.put(info.getSikerId()+""+info.getGroupId(), info);
		}
		CacheUtil.setSikerinfos(map);
	}

}
